from selenium.common.exceptions import NoSuchElementException

from Case.python_ui.Common.basepage import Basepage
import time, pytest
from Case.python_ui.PageLocate.admin_coupon_promo_crowd_locator import AdminCouponPromoCrowdLocator
from Case.python_ui.Common.commontool import CommonTool
from selenium.webdriver.common.action_chains import ActionChains
from Case.python_ui.Test_data.login_data import Logindata
from selenium.webdriver.support import expected_conditions as EC



class AdminCouponPromoCrowdPage(Basepage):

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    #元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        # try:
        #     self.get_element(locate, doc)
        #     return True
        # except:
        #     return False
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    #进入Promo Crowd 菜单页面
    def enter_prpmo_crowd_page(self):
        time.sleep(2)
        doc = "进入Promo Crowd 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        self.wait_elevisble(AdminCouponPromoCrowdLocator.promo_management_menu, doc)
        self.click_element(AdminCouponPromoCrowdLocator.promo_management_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponPromoCrowdLocator.promo_crowd_menu, doc)
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_menu, doc)
        time.sleep(3)

   #创建promo crowd
    def create_promo_crowd(self):
        time.sleep(2)
        promo_crowd_name = 'lwm_' + CommonTool.generate_string()
        doc = "创建promo crowd"
        self.wait_elevisble(AdminCouponPromoCrowdLocator.add_promo_crowd_button, doc)
        self.click_element(AdminCouponPromoCrowdLocator.add_promo_crowd_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponPromoCrowdLocator.promo_crowd_name_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.promo_crowd_name_input, promo_crowd_name, doc)
        self.click_element(AdminCouponPromoCrowdLocator.add_promo_crowd_ok_button, doc)
        time.sleep(2)
        #校验是否创建成功
        self.wait_elevisble(AdminCouponPromoCrowdLocator.search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.search_input, promo_crowd_name, doc)
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.first_search_result, doc).text, promo_crowd_name)
        return promo_crowd_name

    #搜索promo crowd
    def search_promo_crowd(self):
        time.sleep(2)
        doc = "搜索promo crowd"
        fuzzy_query = ['lwm', 'wm_']
        #先创建promo crowd，再搜索，保证有搜索结果
        self.create_promo_crowd()
        #清空搜索框
        self.keyboard_event(AdminCouponPromoCrowdLocator.search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.search_input, fuzzy_query[0], doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_search_button, doc)
        time.sleep(2)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponPromoCrowdLocator.first_search_result, doc).text, fuzzy_query[0])
        time.sleep(2)

        #清空搜索框
        self.keyboard_event(AdminCouponPromoCrowdLocator.search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.search_input, fuzzy_query[1], doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_search_button, doc)
        time.sleep(2)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponPromoCrowdLocator.first_search_result, doc).text, fuzzy_query[1])
        time.sleep(2)

    #新增成员
    def add_promo_crowd_member(self, cmdopt):
        time.sleep(2)
        doc = "新增promo crowd成员"
        #先创建promo crowd，再新增成员，保证有成员可添加
        self.create_promo_crowd()
        #点击搜索出来的promo crowd
        self.click_element(AdminCouponPromoCrowdLocator.first_search_result, doc)
        time.sleep(5)
        member_email = Logindata.user_data[cmdopt]['user']['username']
        #点击新增成员按钮
        self.click_element(AdminCouponPromoCrowdLocator.add_member_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponPromoCrowdLocator.add_member_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.add_member_input, member_email, doc)
        time.sleep(2)
        #点击提交按钮
        self.click_element(AdminCouponPromoCrowdLocator.add_member_ok_button, doc)
        time.sleep(2)
        #校验添加结果
        doc = '搜索刚新增的成员'
        self.wait_elevisble(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.member_search_input, member_email, doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.member_search_button, doc)
        time.sleep(2)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result, doc).text, member_email)
        #校验成员列表字段
        self.check_list_attribute()


    #编辑promo crowd
    def edit_promo_crowd(self):
        time.sleep(2)
        doc = "编辑promo crowd"
        #先创建promo crowd，保证有可编辑的数据
        self.create_promo_crowd()
        #点击搜索出来的promo crowd
        self.click_element(AdminCouponPromoCrowdLocator.first_search_result, doc)
        time.sleep(2)
        #点击action按钮
        self.move_mouse_to_click_element(AdminCouponPromoCrowdLocator.action_button, doc)
        #点击edit按钮
        self.wait_elevisble(AdminCouponPromoCrowdLocator.edit_promo_crowd_button, doc)
        self.click_element(AdminCouponPromoCrowdLocator.edit_promo_crowd_button, doc)
        #修改name 和 description
        self.wait_elevisble(AdminCouponPromoCrowdLocator.edit_name_input, doc)
        new_name = self.get_element(AdminCouponPromoCrowdLocator.edit_name_input, doc).get_attribute('value') + '_edit'
        new_description = 'edit description'
        self.keyboard_event(AdminCouponPromoCrowdLocator.edit_name_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.edit_name_input, new_name, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.edit_description_input, new_description, doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.edit_promo_crowd_ok_button, doc)
        time.sleep(3)
        #校验修改结果
        self.wait_elevisble(AdminCouponPromoCrowdLocator.promo_crowd_name, doc)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.promo_crowd_name, doc).text, new_name)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.promo_crowd_description, doc).text, new_description)

    #删除promo crowd
    def delete_promo_crowd(self):
        time.sleep(2)
        doc = "delete promo crowd"
        #先创建promo crowd，保证有可删除的数据
        self.create_promo_crowd()
        #点击搜索出来的promo crowd
        self.move_mouse_to_click_element(AdminCouponPromoCrowdLocator.first_search_result, doc)
        time.sleep(2)
        #点击action按钮
        self.click_element(AdminCouponPromoCrowdLocator.action_button, doc)
        #点击delete按钮
        self.wait_elevisble(AdminCouponPromoCrowdLocator.delete_promo_crowd_button, doc)
        self.click_element(AdminCouponPromoCrowdLocator.delete_promo_crowd_button, doc)
        time.sleep(2)
        #勾选二次确认
        self.click_element(AdminCouponPromoCrowdLocator.delete_promo_crowd_check_box, doc)
        time.sleep(2)
        #点击ok
        self.click_element(AdminCouponPromoCrowdLocator.delete_promo_crowd_ok_button, doc)
        time.sleep(3)
        #检查是否删除成功
        #删除成功后，再次点击搜索按钮，校验是否有查询结果
        doc = "校验promo crowd是否被删除"
        self.wait_elevisble(AdminCouponPromoCrowdLocator.promo_crowd_search_button, doc)
        self.click_element(AdminCouponPromoCrowdLocator.promo_crowd_search_button, doc)
        time.sleep(5)
        result = self.element_is_exist(AdminCouponPromoCrowdLocator.first_search_result)
        assert not result

    #搜索promo crowd member
    def search_promo_crowd_member(self, cmdopt):
        time.sleep(2)
        doc = "搜索 promo crowd member"
        #先创建添加成员，保证有可搜索的数据
        self.add_promo_crowd_member(cmdopt)
        #获取刚添加的成员信息
        member_email = self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result_email, doc).text
        member_phone = self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result_phone, doc).text
        #邮箱模糊搜索成员
        self.keyboard_event(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.member_search_input, member_email.split('@', 1)[0], doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.member_search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result_email, doc).text, member_email)
        # 手机号码搜索成员
        self.keyboard_event(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.member_search_input, member_phone.split(')', 1)[1], doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.member_search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result_phone, doc).text, member_phone)
        # 手机号码模糊搜索成员
        self.keyboard_event(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.member_search_input, member_phone.split(')', 1)[1][1:8], doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.member_search_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponPromoCrowdLocator.first_member_search_result_phone, doc).text, member_phone)


    #删除promo crowd member
    def delete_promo_crowd_member(self, cmdopt):
        time.sleep(2)
        doc = "删除 promo crowd member"
        #先创建添加成员，保证有可删除的数据
        self.add_promo_crowd_member(cmdopt)
        member_email = self.get_element(AdminCouponPromoCrowdLocator.member_search_input, doc).get_attribute('value')
        # 点击remove member
        self.click_element(AdminCouponPromoCrowdLocator.first_member_search_result_remove_member_button, doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.delete_promo_crowd_member_check_box, doc)
        time.sleep(2)
        self.click_element(AdminCouponPromoCrowdLocator.delete_promo_crowd_member_ok_button, doc)
        time.sleep(3)
        #校验是否删除成功
        #删除成功后，再次点击搜索按钮，校验是否有查询结果
        doc = "校验member是否被删除"
        #先搜索被删除的成员
        self.wait_elevisble(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.keyboard_event(AdminCouponPromoCrowdLocator.member_search_input, doc)
        self.send_keys(AdminCouponPromoCrowdLocator.member_search_input, member_email, doc)
        self.click_element(AdminCouponPromoCrowdLocator.member_search_button, doc)
        time.sleep(5)
        result = self.element_is_exist(AdminCouponPromoCrowdLocator.first_member_search_result)
        assert not result




    #校验列表元素
    #时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        attribute_list = ['Account Email','Account Type','Status','User Name','Phone Number','Creation Time']
        list_attribute_0 = self.get_element(AdminCouponPromoCrowdLocator.member_list_account_email, doc).text
        self.check_contain_str(list_attribute_0, attribute_list[0])
        list_attribute_1 = self.get_element(AdminCouponPromoCrowdLocator.member_list_account_type, doc).text
        self.check_contain_str(list_attribute_1, attribute_list[1])
        list_attribute_2 = self.get_element(AdminCouponPromoCrowdLocator.member_list_status, doc).text
        self.check_contain_str(list_attribute_2, attribute_list[2])
        list_attribute_3 = self.get_element(AdminCouponPromoCrowdLocator.member_list_user_name, doc).text
        self.check_contain_str(list_attribute_3, attribute_list[3])
        list_attribute_4 = self.get_element(AdminCouponPromoCrowdLocator.member_list_phone_number, doc).text
        self.check_contain_str(list_attribute_4, attribute_list[4])
        list_attribute_5 = self.get_element(AdminCouponPromoCrowdLocator.member_list_creation_time, doc).text
        self.check_contain_str(list_attribute_5, attribute_list[5])
        time.sleep(3)












